package com.sunny.ynhz.ui.ynts.problemover;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.sunny.ynhz.R;
import com.sunny.ynhz.adapter.ProblemBJAdapter;
import com.sunny.ynhz.bean.MoreLineProBlemBean;
import com.sunny.ynhz.bean.ProblemStaticBean;
import com.sunny.ynhz.bean.RingBean;
import com.sunny.ynhz.model.IProblemTypeNewResultView;
import com.sunny.ynhz.model.IProblemTypeResultView;
import com.sunny.ynhz.model.ProblemStandModel;
import com.sunny.ynhz.model.ProblemStaticModel;
import com.sunny.ynhz.ui.base.AppBaseTranslationTitleBarActivity;
import com.sunny.ynhz.ui.ynts.WxyyActivity;
import com.sunny.ynhz.ui.ynts.partorlstaticfragment.CityStaticFragment;
import com.sunny.ynhz.ui.ynts.partorlstaticfragment.CunStaticFragment;
import com.sunny.ynhz.ui.ynts.partorlstaticfragment.TownsStaticFragment;
import com.sunny.ynhz.utils.ChartUtils;
import com.sunny.ynhz.utils.DetailsMarkerView;
import com.sunny.ynhz.utils.IntToSmallChineseNumber;
import com.sunny.ynhz.utils.MoveToPosition;
import com.sunny.ynhz.utils.PieChartEntity;
import com.sunny.ynhz.utils.SwitchFragmentUtils;
import com.sunny.ynhz.utils.TimeUtils;
import com.sunny.ynhz.utils.ToastUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by huhuan123 on 2018/8/7.
 *
 * @describe TODO
 */

public class ProblemStaticActivity extends AppBaseTranslationTitleBarActivity implements IProblemTypeNewResultView {
    @BindView(R.id.rl_db)
    RecyclerView rl_db;
    @BindView(R.id.mcharts)
    LineChart lineChart;
    @BindView(R.id.piechart)
    PieChart pieChart;
    @BindView(R.id.ll)
    LinearLayout ll;
    @BindView(R.id.tv_start_time)
    TextView start_time;
    @BindView(R.id.tv_end_time)
    TextView tv_end_time;
    List<ProblemStaticBean.DateBean> mList = new ArrayList<>();
    private ProblemBJAdapter problemBJAdapter;
    List<Integer> mConuntList = new ArrayList<>();
    List<Float> havDoneCountList = new ArrayList<>();
    List<Float> havDonePreList = new ArrayList<>();
    public int[] color = {Color.parseColor("#82E2E6"), Color.parseColor("#FB8DA8"), Color.parseColor("#FFB198"), Color.parseColor("#58D0ED"), Color.parseColor("#AA66CC"), Color.parseColor("#8600FF"), Color.parseColor("#EEB422"), Color.parseColor("#005757"), Color.parseColor("#B22222"), Color.parseColor("#8C8C8C"), Color.parseColor("#7CFC00"), Color.parseColor("#DCB5FF"), Color.parseColor("#99CC3D"),Color.parseColor("#F4BC21"),Color.parseColor("#FE9797"),Color.parseColor("#F28F5E"),Color.parseColor("#82E2E6"), Color.parseColor("#FB8DA8"), Color.parseColor("#FFB198"), Color.parseColor("#58D0ED"), Color.parseColor("#AA66CC"), Color.parseColor("#8600FF"), Color.parseColor("#EEB422"), Color.parseColor("#005757"), Color.parseColor("#B22222"), Color.parseColor("#8C8C8C"), Color.parseColor("#7CFC00"), Color.parseColor("#DCB5FF"), Color.parseColor("#99CC3D"),Color.parseColor("#F4BC21"),Color.parseColor("#FE9797"),Color.parseColor("#F28F5E")};
    public boolean isLoad = false;
    private ProblemStaticModel problemStandModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.city_problem_fragment);
        setTitle("问题办结率");
        ChartUtils.initChart(lineChart, this);
        problemBJAdapter = new ProblemBJAdapter(mList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rl_db.setLayoutManager(linearLayoutManager);
        rl_db.setAdapter(problemBJAdapter);
        tv_end_time.setText(TimeUtils.getNowData());
        start_time.setText(TimeUtils.getCurrentLastMonth());
        problemStandModel = new ProblemStaticModel(this, this);
        problemStandModel.getInstanceMsgList(TimeUtils.getCurrentLastMonth(), TimeUtils.getNowData());

    }


    @Override
    public void onSuccess(Object o) {
        if (o instanceof ProblemStaticBean) {
            mList.clear();
            ProblemStaticBean problemStaticBean = (ProblemStaticBean) o;
            mList.addAll(problemStaticBean.getDate());
            problemBJAdapter.notifyDataSetChanged();
        }
        if (o instanceof MoreLineProBlemBean) {
            MoreLineProBlemBean moreLineProBlemBean = (MoreLineProBlemBean) o;
            List<MoreLineProBlemBean.DateBean> date = moreLineProBlemBean.getDate();
            String[] month = new String[date.size()];
            for (int i = 0; i < date.size(); i++) {
                MoreLineProBlemBean.DateBean dateBean = date.get(i);
                int ycount = dateBean.getYCOUNT();
                havDoneCountList.add((float) ycount);
                int pcount = dateBean.getPCOUNT();
                mConuntList.add(pcount);
                if (pcount == 0) {
                    havDonePreList.add((float) 0);
                } else {
                    float haveDonePre = ycount / (float) pcount;
                    havDonePreList.add(haveDonePre * 100);
                }
                String[] split = dateBean.getMOUTH().split("-");
                month[i] = split[1];
            }

            ChartUtils.showLineChart(mConuntList, "总数", Color.parseColor("#E2B25F"), lineChart, month);
            ChartUtils.addLine(havDoneCountList, "已办结", Color.parseColor("#6EE4A2"), lineChart);
            ChartUtils.addLine(havDonePreList, "办结率", Color.parseColor("#79C6EC"), lineChart);
            DetailsMarkerView detailsMarkerView = new DetailsMarkerView(ProblemStaticActivity.this, R.layout.item_mark_view);
            detailsMarkerView.setChartView(lineChart);
            lineChart.setMarker(detailsMarkerView);
        }
        if (o instanceof RingBean) {
            RingBean ringBean = (RingBean) o;
            List<RingBean.DateBean> date = ringBean.getDate();
            String[] title = new String[date.size()];
            int[] number = new int[date.size()];
            ArrayList<PieEntry> entries = new ArrayList<PieEntry>();
            ll.removeAllViews();
            int total = 0;
            for (int j = 0; j < date.size(); j++) {
                total = date.get(j).getPCOUNT() + total;
            }

            for (int i = 0; i < date.size(); i++) {
                RingBean.DateBean dateBean = date.get(i);
                String label = dateBean.getLABEL();
                int pcount = dateBean.getPCOUNT();
                title[i] = label;
                number[i] = pcount;
                entries.add(new PieEntry(pcount, pcount));
                View view = LayoutInflater.from(ProblemStaticActivity.this).inflate(R.layout.item_piech, null);
                view.findViewById(R.id.view_bg).setBackgroundColor(color[i]);
                TextView tv_lable = view.findViewById(R.id.tv_lable);
                if (total == 0) {
                    tv_lable.setText(label + "(" + 0 + ")");
                } else {
                    tv_lable.setText(label + "(" + TimeUtils.floatSava2(pcount * 100 / (float) total) + "%)");
                }
                ll.addView(view);
            }
            PieChartEntity pieChartEntity = new PieChartEntity(pieChart, entries, title, color, 12f, Color.GRAY, PieDataSet.ValuePosition.OUTSIDE_SLICE);
            pieChartEntity.setHoleEnabled(Color.TRANSPARENT, 60f, Color.TRANSPARENT, 60f);
            pieChartEntity.setLegendEnabled(false);
            pieChartEntity.setPercentValues(true);

        }


    }

    @Override
    public void onCache() {

    }

    @Override
    public void onFail() {
        if (!isLoad) {
            loadFail();
        } else {
            loadOutFail();
        }

    }

    @Override
    public void onStartLoad() {
        if (!isLoad) {
            startLoad();
        } else {
            loadOutStart();
        }

    }

    @Override
    public void onCompleted() {
        isLoad = true;
        loadSuccess();
        loadOnAfter();
    }

    public void displayTimeSelect(int type, String currentTime) {
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();
        String sysYear = TimeUtils.getSysYear();
        int mCurrentMonth = TimeUtils.getCurrentMonth();
        String nowDate = TimeUtils.getNowDay();
        startDate.set(Integer.parseInt(sysYear) - 2, 0, 1);
        endDate.set(Integer.parseInt(sysYear), mCurrentMonth - 1, Integer.parseInt(nowDate));
        TimePickerView pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                String dateString = formatter.format(date);

                if (type == 1) {
                    tv_end_time.setText(dateString);
                } else {
                    start_time.setText(dateString);
                }
            }
        }).setDate(TimeUtils.strToCalendar(currentTime)).setRangDate(startDate, endDate).build();
        pvTime.show();

    }

    @OnClick({R.id.tv_end_time, R.id.tv_start_time, R.id.tv_look_for})
    public void onClickView(View view) {
        if (view.getId() == R.id.tv_end_time) {
            displayTimeSelect(1, tv_end_time.getText().toString());
        } else if (view.getId() == R.id.tv_start_time) {
            displayTimeSelect(0, start_time.getText().toString());
        } else if (view.getId() == R.id.tv_look_for) {
            long startTime = TimeUtils.strTimeToStamp(start_time.getText().toString());
            long endTime = TimeUtils.strTimeToStamp(tv_end_time.getText().toString());
            if (endTime < startTime) {
                ToastUtils.SingleToastUtil(ProblemStaticActivity.this, "开始时间不能大于结束时间");
                return;
            }
            problemStandModel.getMorenLineBJ(start_time.getText().toString(), tv_end_time.getText().toString());
        }
    }

    @Override
    public void resLoad() {
        super.resLoad();
        problemStandModel.getInstanceMsgList(TimeUtils.getNowData(), TimeUtils.getNowData());
    }
}
